package com.zyw.customcalendar.common.util;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class CustomEventBus {

	protected static CustomEventBus customEventBus;

	private ArrayList<Object> contextlist = new ArrayList<>();

	public static CustomEventBus getDefault(){
		if(null == customEventBus){
			customEventBus = new CustomEventBus();
		}
		return customEventBus;
	}

	//�����¼�
	public void post(Object object){
		for(int i = 0; i < contextlist.size(); i++){
			try {
				Object context = contextlist.get(i);
				Class<?> classss = context.getClass();
				Method method=classss.getMethod("onEventMainThread", object.getClass());
				method.setAccessible(true);
				method.invoke(context, object);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//���͵�ָ������ķ�����
	public void post(String activityName, Object object){
		for(int i = 0; i < contextlist.size(); i++){
			try {
				Class<?> classss = contextlist.get(i).getClass();
				if(activityName != null && activityName.equals(classss.getName())){
					Method method=classss.getMethod("onEventMainThread", Object.class);
					method.setAccessible(true);
					method.invoke(classss, object);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void register(Object cObject){
		contextlist.add(cObject);
	}

	public void unregister(Object cObject){
		contextlist.remove(cObject);
	}
}
